/*
 * @Description 
 * @Autor lzhiqiang
 * @Date 2023-03-05 20:10:11
 * @LastEditors lzhiqiang
 * @LastEditTime 2023-04-04 11:05:47
 */
#include "johnsonli.h"
#include "iomanager.h"

#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/types.h>

static johnsonli::Logger::ptr g_logger = LOG_ROOT();

void test_fiber1()
{
    LOG_INFO(g_logger) << "test fiber";
}

void test01()
{

    LOG_INFO(g_logger) << "timer test ";

    // johnsonli::IOManager iom;
    // iom.schedule(&test_fiber1);

    // int cfd = socket(AF_INET, SOCK_STREAM, 0);
    // fcntl(cfd, F_SETFL, O_NONBLOCK);

    // sockaddr_in addr;
    // memset(&addr, 0, sizeof(sockaddr_in));
    // addr.sin_family = AF_INET;
    // addr.sin_port = htons(80);
    // inet_pton(AF_INET, "180.101.50.188", &addr.sin_addr.s_addr);

    // iom.addEvent(cfd, johnsonli::IOManager::WRITE, [](){
    //     LOG_INFO(g_logger) << "connected";
    // });

    // connect(cfd, (const sockaddr*)&addr, sizeof(addr));
}

static johnsonli::Timer::ptr s_timer;
void test_timer()
{
    johnsonli::IOManager iom(1);
    //iom.schedule(&test_fiber1);
    
    s_timer = iom.addTimer(1000, [](){
        static int i = 0;

        LOG_INFO(g_logger) << "timer test " << i;
        if(++i == 3)
        {
            //s_timer->refresh();
            //s_timer->reset(2000, true);
            s_timer->cancel();
        }
        
    }, true);

}

void test_iom()
{
    LOG_INFO(g_logger) << "begin";
    sleep(2);
    LOG_INFO(g_logger) << "end";
}

int main()
{
    johnsonli::IOManager iom(1);
    iom.schedule(test_iom);

    return 0;
}